function[WCF2,WSQ2, Weffi] = welfare_fun(side, CFvec, cf, flexible, plots)

global foldermats folderfig cols eta homedealer  kappa quantityrobus day loyaltyinW keepall largetrade tradesize fej zerobenefit loyalty
cols = [3,9,4,1,2,5,8,6,7];  
CF=CFvec(cf);

%Note: This code runs for  homedealer=1, eta=0, xij_for_investor=1, homedealer=1, pool =1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LOAD COUNTERFACUTAL 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if zerobenefit==0
    %Without controlling for the fact that retailers trade with less dealers in SQ
    temp=['/counterfac_',[num2str(side),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize),'_' num2str(CF),'_',num2str(flexible), '_', num2str(fej), '_', num2str(loyalty)]];
else 
    temp=['/counterfac_',[num2str(side),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize),'_' num2str(CF),'_',num2str(flexible), '_', num2str(fej), '_', num2str(loyalty), '_zerobenefit']];
end 
    load(fullfile([foldermats,temp,'.mat']))
     
colI = 1:Imax;
colR = (Imax+1):2*Imax; 
qcf21_false=NaN;

%CF quote
qCF =qcf21;
    
%Expected surplus and profit         
EUCF_I = uBcf21_g(:,colI)+uEcf21_g(:,colI) + Ecostcf21_g(:,colI);
EUCF_R = uBcf21_g(:,colR)+uEcf21_g(:,colR) + Ecostcf21_g(:,colR);
PICF_I = piBcf21_g(:,colI)+piEcf21_g(:,colI);
PICF_R = piBcf21_g(:,colR)+piEcf21_g(:,colR);
PICF   = kappa*PICF_R+(1-kappa)*PICF_I;
EUCF   = kappa*EUCF_R+(1-kappa)*EUCF_I;

%Expected yield
EYCF     = kappa*(YEcf21_g(:,colR)+YBcf21_g(:,colR))+(1-kappa)*(YEcf21_g(:,colI)+YBcf21_g(:,colI));

%expected profit plus utility 
WCF         = nansum(PICF+EUCF ,2) ;
WCF_I       = nansum(PICF_I+EUCF_I,2);
WCF_R       = nansum(PICF_R+EUCF_R,2);

%expected welfare
if loyaltyinW==0 || homedealer==0
    WCF2_I     = Wcf21_g(:,1);
    WCF2_R     = Wcf21_g(:,2);
    WCF2b_I    = Wcf21_shrink_g(:,1);
    WCF2b_R    = Wcf21_shrink_g(:,2);

    WCFxij_I  = Wxijcf21_g(:,1);
    WCFxij_R  = Wxijcf21_g(:,2);
    WCFxijH_I = WxijHcf21_g(:,1);
    WCFxijH_R = WxijHcf21_g(:,2);
    WCFvdj_I  = Wvdjcf21_g(:,1);
    WCFvdj_R  = Wvdjcf21_g(:,2);
else

    WCF2_I     = Wcf21_gwH(:,1);
    WCF2_R     = Wcf21_gwH(:,2);
    WCF2b_I    = Wcf21_shrink_gwH(:,1);
    WCF2b_R    = Wcf21_shrink_gwH(:,2);

    WCFxij_I  = Wxijcf21_gwH(:,1);
    WCFxij_R  = Wxijcf21_gwH(:,2);
    WCFxijH_I = WxijHcf21_gwH(:,1);
    WCFxijH_R = WxijHcf21_gwH(:,2);
    WCFvdj_I  = Wvdjcf21_gwH(:,1);
    WCFvdj_R  = Wvdjcf21_gwH(:,2);
    
end 

WCF2      = (1-kappa)*WCF2_I    + kappa*WCF2_R;
WshrinkCF2= (1-kappa)*WCF2b_I  + kappa*WCF2b_R;
WCFxij    = (1-kappa)*WCFxij_I  + kappa*WCFxij_R;
WCFxijH   = (1-kappa)*WCFxijH_I + kappa*WCFxijH_R;
WCFvdj    = (1-kappa)*WCFvdj_I  + kappa*WCFvdj_R;

rhojcf21_I     = rhojcf21_g(:,colI);
rhojcf21_opt_I = rhojcf21_opt_g(:,colI);
sjcf21_I       = sjcf21_g(:,colI);
s0jcf21_I      = s0jcf21_g(:,colI);

rhojcf21_R     = rhojcf21_g(:,colR);
rhojcf21_opt_R = rhojcf21_opt_g(:,colR);
sjcf21_R       = sjcf21_g(:,colR);
s0jcf21_R      = s0jcf21_g(:,colR);

if homedealer==0
     sHjcf21_I    = sjcf21_g(:,colI); 
     sHjcf21_R    = sjcf21_g(:,colR); 
else 
    sHjcf21_I       = sHjcf21_g(:,colI);
    sHjcf21_R     = sHjcf21_g(:,colR);
end 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LOAD SQ WITH RETAILERS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

retailer=1;
if zerobenefit==0
    temp=['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty)]]; 
else 
   temp=['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty), '_zerobenefit']]; 
end 
load(fullfile([foldermats,temp,'.mat']))

PISQ_R    = PiSQ;
EUSQ_R    = uBSQ;
  
rhojSQ_R  = rhojSQ;
if homedealer==0, sHjSQ_R    = sjSQ; else sHjSQ_R    = sHjSQ;end 

if loyaltyinW==0 || homedealer==0
    WSQ2_R   = WSQ;
    WshrinkSQ2_R = WshrinkSQ;
    WSQxij_R  = WxijSQ;
    WSQxijH_R = WxijHSQ;
    WSQvdj_R  = WvdjSQ;
else 
    WSQ2_R   = WSQwH;
    WshrinkSQ2_R = WshrinkSQwH;
    WSQxij_R  = WxijSQwH;
    WSQxijH_R = WxijHSQwH;
    WSQvdj_R  = WvdjSQwH;

end 

if CFvec(cf)==6||CFvec(cf)==10||CFvec(cf)==14 %in the counterfact with r=0, use the welfare without r
    WSQ2_R       = WSQ;
    WshrinkSQ2_R = WshrinkSQ;
    WSQxij_R     = WxijSQ;
    WSQxijH_R    = WxijHSQ;
    WSQvdj_R     = WvdjSQ;   
end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LOAD SQ WITH INSTITUIONALS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

retailer=0;
if zerobenefit==0
    temp=['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty)]];  
else 
   temp=['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty), '_zerobenefit']];  
end 
load(fullfile([foldermats,temp,'.mat']))
    
    %compute best dealer -- Note: this is no longer used. Only works with certain specifications of the model
    if zerobenefit==0
        input_file=fullfile([foldermats,['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty)]],'.mat']) ;
    else
        input_file=fullfile([foldermats,['/counterfac_SQ_',[num2str(side),'_' num2str(retailer), '_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej), '_', num2str(loyalty), '_zerobenefit']],'.mat']) ;
    end
    [idx_best, dealer_best, Weffi] = allocative_efficient(input_file); 
  

q_data_I  =q_data; 
etaEjSQ_I = etaEjSQ;
etaDjSQ_I = etaDjSQ;
        
PISQ_I    = PiSQ ;
EUSQ_I    = uBSQ +uESQ;
rhojSQ_I  = rhojSQ;
if homedealer==0, sHjSQ_I    = sjSQ; else sHjSQ_I    = sHjSQ;end 

if loyaltyinW==0 || homedealer==0
    WSQ2_I       = WSQ;
    WshrinkSQ2_I = WshrinkSQ;
    WSQxij_I     = WxijSQ;
    WSQxijH_I    = WxijHSQ;
    WSQvdj_I     = WvdjSQ;
else
    WSQ2_I       = WSQwH;
    WshrinkSQ2_I = WshrinkSQwH;
    WSQxij_I     = WxijSQwH;
    WSQxijH_I    = WxijHSQwH;
    WSQvdj_I     = WvdjSQwH;
end 

if CFvec(cf)==6||CFvec(cf)==10||CFvec(cf)==14 %in the counterfact with r=0, use the welfare without r
    WSQ2_I       = WSQ;
    WshrinkSQ2_I = WshrinkSQ;
    WSQxij_I     = WxijSQ;
    WSQxijH_I    = WxijHSQ;
    WSQvdj_I     = WvdjSQ;   
end 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% COMBINE/CALCULATE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%Compute welfare:
PISQ     = kappa*PISQ_R+(1-kappa)*PISQ_I;
EUSQ     = kappa*EUSQ_R+(1-kappa)*EUSQ_I;

%number of active dealers (assuming same number per retail and institution - this is an approxmiation!) 
Jt=kappa*sum(~isnan(EUSQ_R),2)+(1-kappa)*sum(~isnan(EUSQ_I),2);
avgJt = mean(Jt(Jt~=0)); % expected mass of investors


WSQ    = nansum(PISQ+EUSQ,2);
WSQ_I  = nansum(PISQ_I+EUSQ_I ,2);
WSQ_R  = nansum(PISQ_R+EUSQ_R,2);
    
WSQ2        = (1-kappa)*WSQ2_I   + kappa*WSQ2_R;
WshrinkSQ2  = (1-kappa)*WshrinkSQ2_I   + kappa*WshrinkSQ2_R;
WSQxij      = (1-kappa)*WSQxij_I + kappa*WSQxij_R;
WSQxijH     = (1-kappa)*WSQxijH_I + kappa*WSQxijH_R;
WSQvdj      = (1-kappa)*WSQvdj_I + kappa*WSQvdj_R;
     
%Welfare decomposition 
if homedealer==0
    WDcomp_I    =(WCFxij_I-WSQxij_I)./(WCFxij_I + WCFvdj_I - (WSQxij_I+WSQvdj_I))*100;
    WDcomp_R    =(WCFxij_R-WSQxij_R)./(WCFxij_R + WCFvdj_R - (WSQxij_R+WSQvdj_R))*100;
    WDcomp      =(WCFxij-WSQxij)./(WCFxij + WCFvdj - (WSQxij+WSQvdj))*100;
    WDcompv     =(WCFvdj-WSQvdj)./(WCFxij + WCFvdj - (WSQxij+WSQvdj))*100;
    WDcomp_both =[WDcomp_I,WDcomp_R, WDcomp,WDcompv];
else 
    DW      = WCF2    - WSQ2;       
    DW_xij  = WCFxij  - WSQxij;     %basequality
    DW_xijH = WCFxijH - WSQxijH;    %homedealer benefit 
    DW_vdj  = WCFvdj  - WSQvdj;     %dealer value

    %checking:
    DW_I      = WCF2_I    - WSQ2_I;       
    DW_xij_I  = WCFxij_I  - WSQxij_I;     %basequality
    DW_xijH_I = WCFxijH_I - WSQxijH_I;    %homedealer benefit 
    DW_vdj_I  = WCFvdj_I  - WSQvdj_I;     %dealer value

    DW_R      = WCF2_R    - WSQ2_R;       
    DW_xij_R  = WCFxij_R  - WSQxij_R;     %basequality
    DW_xijH_R = WCFxijH_R - WSQxijH_R;    %homedealer benefit 
    DW_vdj_R  = WCFvdj_R  - WSQvdj_R;     %dealer value

    lines=find(~isnan(DW));
    
end 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% DISPLAY FINDINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

    %Change in quote
    disp('----------------------------------------------------------------')
    disp('QUOTE')
    disp('----------------------------------------------------------------')
    disp(['Increase in median quote:', num2str(nanmedian(nanmedian(qCF(:,cols)-q_data_I(:,cols))))]);
    disp(['Median quote is:', num2str(nanmedian(nanmedian(q_data_I(:,cols))))]);

    
    %Change in expected surplus and profit
    disp('----------------------------------------------------------------')
    disp('PROFIT AND UTILITY')
    disp('----------------------------------------------------------------')
    disp(['Expected profit in SQ per trade for the dealer from retail investors is ', num2str(nanmedian(nanmean(PISQ_R,2)))])
    disp(['Expected profit in CF per trade for the dealer from retail investors is ', num2str(nanmedian(nanmean(PICF_R,2)))])
    
    disp(['Expected surplus in SQ per trade for the retail investor is ', num2str(nanmedian(nanmean(EUSQ_R,2)))])
    disp(['Expected surplus in CF per trade for the retail investor is ', num2str(nanmedian(nanmean(EUCF_R,2)))])

    disp(['Expected profit in SQ per trade for the dealer from institutional is ', num2str(nanmedian(nanmean(PISQ_I,2)))])
    disp(['Expected profit in CF per trade for the dealer from institutional is ', num2str(nanmedian(nanmean(PICF_I(PICF_I~=-Inf),2)))])
    
    disp(['Expected surplus in SQ per trade for the institutional investor is ', num2str(nanmedian(nanmean(EUSQ_I,2)))])
    disp(['Expected surplus in CF per trade for the institutional investor is ', num2str(nanmedian(nanmean(EUCF_I,2)))])


    %Change in welfare for average trade 
    disp('----------------------------------------------------------------')
    disp('WELFARE')
    disp('----------------------------------------------------------------')
    disp(['Total Expected welfare:  ', num2str(nanmean(WCF2)), ' bps'])
    disp(['Total Expected welfare:  ', num2str(nanmean(WshrinkCF2)), ' bps when shrinking dealer values '])

    disp(['Expected welfare changes by ', num2str(nanmean(WCF2 - WSQ2)), ' bps', 'out of ', num2str(nanmean( WSQ2)), ' bps'])
    disp(['Expected welfare changes by ', num2str(nanmean(WshrinkCF2 - WshrinkSQ2)), ' bps when shrinking dealer values out of ', num2str(nanmean(WshrinkSQ2)), ' bps'])

    diffW=nanmean((WCF2 - WSQ2)./WSQ2,2).*100; %average over dealers
    diffWshrink=nanmean((WshrinkCF2 - WshrinkSQ2)./WshrinkSQ2,2).*100; %average over dealers
    disp(['Expected welfare changes by ', num2str(nanmean(diffW(diffW~=-Inf))), '%.'])
    

    %Welfare decomposition with homedealer
    if homedealer==1
  
        disp([num2str(nanmean(DW_xijH./DW*100)), ' % of the welfare change comes from the homedealer benefit'])
        disp([num2str(nanmean(DW_vdj./DW*100)), ' % of the welfare change comes from matching to  dealers with higher values'])    
        disp([num2str(nanmean(DW_xij./DW*100)), ' % of the welfare change comes from matching to  dealers with better basequality values'])    
 
    end 
    disp(['Expected welfare changes by ', num2str(nanmean(diffWshrink(diffWshrink~=-Inf))), '% when shrinking dealer values.'])


    %Platform participation
    disp('----------------------------------------------------------------')
    disp('TRADING')
    disp('----------------------------------------------------------------')
    disp(['The probability that a retailer enters the platform is ', num2str(nanmedian(nanmean(100-rhojSQ_R*100,2))), ' in the SQ'])
    disp(['The probability that a retailer enters the platform is ', num2str(nanmedian(nanmean(100-rhojcf21_R*100,2))), ' in the CF'])
    disp(['The probability that an institutional enters the platform is ', num2str(nanmedian(nanmean(100-rhojSQ_I*100,2))), ' in the SQ'])
    disp(['The probability that an institutional enters the platform is ', num2str(nanmedian(nanmean(100-rhojcf21_I*100,2))), ' in the CF'])


    disp(['Probability to trade with the home dealer in SQ for I dealer is :  ', num2str(nanmean(nanmean(rhojSQ_I + sHjSQ_I))*100), '%'])
    disp(['Probability to trade with the home dealer in CF for I dealer is :  ', num2str(nanmean(nanmean(rhojcf21_I + sHjcf21_I))*100), '%'])
    disp(['Probability to trade with the home dealer in SQ for R dealer is : ', num2str(nanmean(nanmean(rhojSQ_R+sHjSQ_R))*100), '%'])
    disp(['Probability to trade with the home dealer in CF for R dealer is : ', num2str(nanmean(nanmean(rhojcf21_R+sHjcf21_R))*100), '%'])

    disp('----------------------------------------------------------------')
    disp('Welfare table')
    disp('----------------------------------------------------------------')

    if loyaltyinW==1
        output = (nanmean(WCF2)-62.0007)/62.0007*100;
        outputhrink = (nanmean(WshrinkCF2)-57.4838)/57.4838*100;
    else 
        output = (nanmean(WCF2)-36.6132)/36.6132*100;
        outputhrink = (nanmean(WshrinkCF2)-32.0963)/32.0963*100;
    end 
    disp(['Welfare change relative to decentralized market: ', num2str(output), '%'])
    disp(['Welfare change relative to decentralized market when shrinkng vDs: ', num2str(outputhrink), '%'])
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FIGURES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  

if plots==1      
CF=CFvec(cf); 

%Change in quotes 
change_q =  qCF(:,cols)-q_data_I(:,cols);
xlim=get(gca,'xlim');
figure
[change_q_ro] = rmoutliers(change_q); %, 'percentiles',[1,99]);
boxplot(change_q_ro, 'Symbol','+k', 'Color', 'k')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('basis points')
title('increase in quote')
set(gca,'FontSize',16)
temp=['/graph_quotes_pool_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))
 
%Show that quotes of ``representative investor" are very similar to fully sophistiacted quotes
figure
histogram(qcf21_false-qcf21,  100,  'FaceAlpha',.1, 'FaceColor', 'black', 'Normalization', 'pdf')
set(gca,'FontSize',22)
temp=['/graph_quotes_diff_pool_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))
 

%Change in Welfare
%all groups
diffW_both = [(WCF2 - WSQ2)./WSQ2*100];
figure
[change_W_ro] = rmoutliers(diffW_both); %,'percentiles',[1,99]);
boxplot(change_W_ro, 'Label', 'Welfare W$$_t$$', 'Symbol','+k', 'Color', 'k')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('%')
%title('Increase in expected gains of trade')
set(gca,'FontSize',22)
bp = gca;
bp.XAxis.TickLabelInterpreter = 'latex';
temp=['/graph_W_all_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

%all groups, with dealer values that are shrinked
diffW_both = [(WshrinkCF2 - WshrinkSQ2)./WshrinkSQ2*100];
figure
[change_W_ro] = rmoutliers(diffW_both); %,'percentiles',[1,99]);
boxplot(change_W_ro, 'Label', 'Welfare W$$_t$$', 'Symbol','+k', 'Color', 'k')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('%')
%title('Increase in expected gains of trade')
set(gca,'FontSize',22)
bp = gca;
bp.XAxis.TickLabelInterpreter = 'latex';
temp=['/graph_W_all_version_shrink',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

%all groups
diffW_both = [(WCF - WSQ)./WSQ *100];
figure
[change_W_ro] = rmoutliers(diffW_both); %,'percentiles',[1,99]);
boxplot(change_W_ro, 'Label', 'Welfare W$$_t$$', 'Symbol','+k', 'Color', 'k')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('%')
%title('Increase in expected gains of trade')
set(gca,'FontSize',22)
bp = gca;
bp.XAxis.TickLabelInterpreter = 'latex';
temp=['/graph_W_all_2_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))
 

%Increase in probability that a randomly drawn investor of group g buys from the best dealer
rows=find(~isnan(idx_best));
change_prob_R=NaN*ones(size(idx_best,1),1);
change_prob_I=NaN*ones(size(idx_best,1),1);
for i=1:size(rows,1)
    r=rows(i);
    change_prob_R(r) = (rhojcf21_R(r,idx_best(r)) + sjcf21_R(r,idx_best(r)))*100/Imax -(1)*100/Imax;
    change_prob_I(r) = (rhojcf21_I(r,idx_best(r)) + sjcf21_I(r,idx_best(r)))*100/Imax - (rhojSQ_I(r,idx_best(r)) + sjSQ_I(r,idx_best(r)))*100/Imax;
end 
figure
boxplot(rmoutliers([change_prob_I, change_prob_R]),'Color', 'k', 'Label',{'Institutional', 'Retail'}, 'Symbol','+k') % 'Symbol', 'ow',
xlim=get(gca,'xlim');
ylabel('%')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
set(gca,'FontSize',24)
temp=['/graph_prob_R_best_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Welfare decomposition 
figure
[change_W_ro] = rmoutliers(WDcomp_both(:,1:(end-1)));%,'percentiles',[1,99]);
boxplot(change_W_ro, 'Label', {'I','R', 'Both'}, 'Symbol','+k', 'Color', 'k')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('%')
%title('Increase in expected gains of trade')
set(gca,'FontSize',22)
bp = gca;
bp.XAxis.TickLabelInterpreter = 'latex';
temp=['/graph_W_dec_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Increase in probability that a randomly drawn investor of group g buys from  dealer j 
figure
boxplot(rmoutliers((rhojcf21_R(:,cols) + sjcf21_R(:,cols))*100/Imax-(1)*100/Imax),'Color', 'k', 'Label',{'d1', 'd2', 'd3', 'd4', 'd5', 'd0','d7','d8','d9'}, 'Symbol','+k') 
ylim([-20 40])
xlim=get(gca,'xlim');
ylabel('%')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
set(gca,'FontSize',24)
temp=['/graph_prob_R_better_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Inefficiently high entrance to the platform
figure
boxplot([nanmean((1-rhojcf21_I(:,cols))-(1-rhojcf21_opt_I(:,cols)),2)*100,nanmean( (1-rhojcf21_R(:,cols) -(1-rhojcf21_opt_R(:,cols))),2)*100], 'Color', 'k','Label', {'Institutional', 'Retailer'}, 'Symbol','+k') % 'Symbol', 'ow',
xlim=get(gca,'xlim');
ylabel('%')
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
set(gca,'FontSize',24)
temp=['/graph_prob_overshoot_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

 
%Winners and losers
diffPI_R=nanmean(PICF_R - PISQ_R,2); %average over dealers
diffPI_I=nanmean(PICF_I - PISQ_I,2); %average over dealers
diffEU_R=nanmean(EUCF_R - EUSQ_R,2); %average over dealers
diffEU_I=nanmean(EUCF_I - EUSQ_I,2); %average over dealers
diff= [diffEU_R, diffPI_R, diffEU_I, diffPI_I ];

%put in one graph
X = [diff];
color2 =[ones(size(X,2), 1), ones(size(X,2), 1), ones(size(X,2), 1)];
color1 =[zeros(size(X,2), 1),zeros(size(X,2), 1), zeros(size(X,2), 1)] ; 

figure 
x = 1:size(X,2);
boxplot(rmoutliers(X) , x, 'Symbol','+k', 'Color', 'k', 'Label', {'EU$$^R_t$$', 'PI$$^R_t$$', 'EU$$^I_t$$', 'PI$$^I_t$$'})

h = findobj(gca,'Tag','Box');
for j=1:length(h)
    if rem(j,2)==1
    patch(get(h(j),'XData'),get(h(j),'YData'),color1(j,:),'FaceAlpha',.1);
    else 
    patch(get(h(j),'XData'),get(h(j),'YData'),color2(j,:),'FaceAlpha',.3);
    end
end
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
ylabel('basis points') 
set(gca,'FontSize',20)
bp = gca;
bp.XAxis.TickLabelInterpreter = 'latex';
temp=['/graph_winners_bps_all_version',[num2str(side),'_' num2str(CF),'_', num2str(eta)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

end  
end   